package com.app.jzgdirector.base.activity;


import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.app.jzgdirector.common.view.ProgressBarDialog;
import com.common.baselibrary.ApplicationManager;
import com.app.jzgdirector.common.util.ToastUtils;

import butterknife.ButterKnife;

/**
 * Created by80002037 on 2017/3/31 13:04.
 */

public abstract class BaseFragment extends Fragment {
    protected ToastUtils toastUtils;
    private ProgressBarDialog myProgressBarDialog;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (layoutId() != 0) {
            return inflater.inflate(layoutId(), container,false);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        toastUtils=new ToastUtils(getActivity());
        initView(view);
    }

    protected void initView(View view) {

    }

    protected abstract int layoutId();

    /**
     * 显示等待进度条
     */
    public void showLoadingProgressBar() {
        try {
            showLoadingProgressBar(null, false, "");
        } catch (Exception e) {
        }
    }

    /**
     * 显示等待进度条
     *
     * @param needShowWithBg 是否需要显示背景
     * @param loadingHint    显示背景时，可设置文本提示内容
     */
    public void showLoadingProgressBar(final ProgressBarDialog.OnDialogCancelListener
                                               onDialogCancelListener, final boolean needShowWithBg, final String loadingHint) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            ApplicationManager.getMainHandler().post(new Runnable() {
                @Override
                public void run() {
                    showLoadingProgressBar(onDialogCancelListener, needShowWithBg, loadingHint);
                }
            });
            return;
        }

        if (myProgressBarDialog == null) {
            myProgressBarDialog = new ProgressBarDialog(getActivity());
            myProgressBarDialog.setOnCancelListener(onDialogCancelListener);
        }
        if (!myProgressBarDialog.isShowing()) {
            myProgressBarDialog.show(needShowWithBg, loadingHint);
        }
    }

    /**
     * 取消菊花样式的进度条显示
     */
    public void cancelLoadingProgressBar() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            ApplicationManager.getMainHandler().post(new Runnable() {
                @Override
                public void run() {
                    cancelLoadingProgressBar();
                }
            });
            return;
        }
        if (null != myProgressBarDialog && myProgressBarDialog.isShowing()) {
            myProgressBarDialog.manualCancel();
        }
    }

}
